package com.xu.common.ui.activity;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 12708 on 2018/5/27.
 */

public abstract class Activity extends AppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindows();
        if (initArgs(getIntent().getExtras())) {
            //ui布局
            int getlayId = getContentLayoutId();
            setContentView(getlayId);
            //UI初始化
            initWidget();
            //数据
            initData();

        } else
            finish();


    }

    public void initWindows() {

    }

    /**
     * Bundle初始化相关参数
     *
     * @param bundle
     * @return
     */
    public boolean initArgs(Bundle bundle) {
        return true;

    }

    protected abstract int getContentLayoutId();

    /**
     * 控件数据初始化
     */
    protected void initData() {

    }

    /**
     * 控件初始化
     */
    protected void initWidget() {
        ButterKnife.bind(this);

    }

    /**
     * 导航键
     *
     * @return
     */
    @Override
    public boolean onSupportNavigateUp() {
        //当点击返回上一界面时，finish当前界面


        finish();
        return super.onSupportNavigateUp();
    }

    /**
     * 手机Home键
     */
    @Override
    public void onBackPressed() {
        //当
        List<Fragment> fragments = new ArrayList<>();
        if (fragments != null && fragments.size() > 0) {
            for (Fragment fragment : fragments) {
                if (fragment instanceof com.xu.common.ui.fragment.Fragment) {
                    if (((com.xu.common.ui.fragment.Fragment) fragment).onBackPress()) {
                        return;
                    }
                }
            }

        }

        super.onBackPressed();
        finish();
    }
}
